package 练习04_线程池实现穷举法破解密码;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @ClassName: PassWordThread
 * @Author: AllenSun
 * @Date: 2022/3/18 下午9:04
 */
public class PassWordThreadpool {

    //第一步：把穷举法生成密码并且进行匹配的方法写好
    //如果匹配到密码了就停止遍历
    private boolean found = false;

    public synchronized void generatePwd(char[] guessPwd,String pwd, List<String> pwdList) {
        generatePwd(guessPwd,0,pwd, pwdList);
    }

    public synchronized void generatePwd(char[] guessPwd,int index,String pwd, List<String> pwdList) {
        //遍历数值和字母来生成密码
        if(found){
            return;
        }
        for (short i='0';i<'z';i++) {
            if(!Character.isLetterOrDigit(i)){
                continue;
            }
            char c = (char) i;
            guessPwd[index] = c;
            if(index==pwd.length()-1){
                //把三个字母的数组拼接成字符串
                String guessResult = new String(guessPwd);
                pwdList.add(guessResult);
                if(guessResult.equals(pwd)){
                    System.out.println("密码找到了，是："+guessResult);
                    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    System.out.println("当前时间是："+df.format(new Date()));
                    found = true;
                    return;
                }
            } else {
                generatePwd(guessPwd,index+1,pwd, pwdList);
            }
        }


    }

}
